﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using System.Data;
using Actuals.Data;



namespace Actuals.Data
{
    public class DalTechnicalTask : Dal<ITechnicalTask, TechnicalTask>, IDalTechnicalTask
    {


        private readonly IDalManager _manager;
        private static readonly IDalTechnicalTask _instance = new DalTechnicalTask();
        private const string SP_ADD_TECHNICAL_TASK = "usp_AddTechnicalTask";
        private const string SP_GET_TECHNICAL_TASK = "usp_GetTechnical_Task";
        private const string SP_GET_TECHNICAL_TASKS = "usp_GetTechnical_Tasks";
        private const string SP_DELETE_TECHNICAL_TASK = "usp_DeleteTechnical_Task";
        private const string SP_GET_TECHTASK_WITH_USERSTORYID = "usp_GetTechTask_WithUserStoryId";
        private const string SP_UPDATE_TECHNICALTASK = "usp_UpdateTechnicalTask";
        private const string SP_GETTECHTASK_WITH_SPRINTID = "usp_Get_TechTask_With_SprintId";
        private const string SP_UPDATE_INCOMPLETE_TECHNICALTASK = "usp_Update_Incomplete_TechnicalTask";

        private const string SP_UPDATE_TECHNICALTASK_STATUS = "usp_UpdateTechnicalTaskStatus";

        public static IDalTechnicalTask Instance
        {
            get { return _instance; }
        }

        public DalTechnicalTask()
        {
            _manager = new DalManager();
        }

        public DalTechnicalTask(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(ITechnicalTask obj, DataRow row)
        {
            obj.TechnicalTaskId = _manager.GetValue<int>(row, "TechnicalTaskId");
            obj.UserStoryId = _manager.GetValue<int>(row, "UserStoryId");
            obj.UserStoryDescription = _manager.GetValue<string>(row, "UserStoryDescription");
            obj.SprintId = _manager.GetValue<int>(row, "SprintId");
            obj.Status = _manager.GetValue<string>(row, "Status");
            obj.TaskCompletedBy = _manager.GetValue<string>(row, "TaskCompletedBy");
            obj.CodeReviewStatus = _manager.GetValue<string>(row, "CodeReviewStatus");
            obj.CodeReviewDoneBy = _manager.GetValue<string>(row, "CodeReviewDoneBy");
            obj.EstimatedTime = _manager.GetValue<int>(row, "Estimation");
            obj.RemainingTime = _manager.GetValue<int>(row, "RemainingTime");
            obj.TotalElapsedTime = _manager.GetValue<int>(row, "TotalElapsedTime");
            obj.TimeSpent = _manager.GetValue<int>(row, "TimeSpent");


        }
        public int AddTechnicalTask(ITechnicalTask technicalTask)
        {
            _manager.StoredProcedureName = SP_ADD_TECHNICAL_TASK;
            _manager.AddInParameter("@TechnicalTaskId", DbType.Int32, technicalTask.TechnicalTaskId);
            _manager.AddInParameter("@UserStoryId", DbType.Int32, technicalTask.UserStoryId);
            _manager.AddInParameter("@UserStoryDescription", DbType.String, technicalTask.UserStoryDescription);
            _manager.AddInParameter("@SprintId", DbType.Int32, technicalTask.SprintId);
            _manager.AddInParameter("@Status", DbType.String, technicalTask.Status);
            _manager.AddInParameter("@TaskCompletedBy", DbType.String, technicalTask.TaskCompletedBy);
            _manager.AddInParameter("@CodeReviewStatus", DbType.String, technicalTask.CodeReviewStatus);
            _manager.AddInParameter("@CodeReviewDoneBy", DbType.String, technicalTask.CodeReviewDoneBy);            
            _manager.AddInParameter("@EstimatedTime", DbType.Int32, technicalTask.EstimatedTime);
            _manager.AddInParameter("@RemainingTime", DbType.Int32, technicalTask.RemainingTime);
            _manager.AddInParameter("@TotalElapsedTime", DbType.Int32, technicalTask.TotalElapsedTime);
            _manager.AddInParameter("@TimeSpent", DbType.Int32, technicalTask.TimeSpent);



            return _manager.ExecuteNonQuery();


        }
        public int UpdateTechnicalTask(ITechnicalTask technicalTask)
        {
            _manager.StoredProcedureName = SP_UPDATE_TECHNICALTASK;
            _manager.AddInParameter("@TechnicalTaskId", DbType.Int32, technicalTask.TechnicalTaskId);
            _manager.AddInParameter("@SprintId", DbType.Int32, technicalTask.SprintId);
            _manager.AddInParameter("@Status", DbType.String, technicalTask.Status);
            _manager.AddInParameter("@TaskCompletedBy", DbType.String, technicalTask.TaskCompletedBy);
            _manager.AddInParameter("@CodeReviewStatus", DbType.String, technicalTask.CodeReviewStatus);
            _manager.AddInParameter("@CodeReviewDoneBy", DbType.String, technicalTask.CodeReviewDoneBy);
            _manager.AddInParameter("@UserStoryDescription", DbType.String, technicalTask.UserStoryDescription);
            _manager.AddInParameter("@UserStoryId", DbType.Int32, technicalTask.UserStoryId);
            _manager.AddInParameter("@EstimatedTime", DbType.Int32, technicalTask.EstimatedTime);
            _manager.AddInParameter("@RemainingTime", DbType.Int32, technicalTask.RemainingTime);
            _manager.AddInParameter("@TotalElapsedTime", DbType.Int32, technicalTask.TotalElapsedTime);
            _manager.AddInParameter("@TimeSpent", DbType.Int32, technicalTask.TimeSpent);
            return _manager.ExecuteNonQuery();
        }

        public int UpdateTechTaskStatus(ITechnicalTask technicalTask)
        {
            _manager.StoredProcedureName = SP_UPDATE_TECHNICALTASK_STATUS;
            _manager.AddInParameter("@TechnicalTaskId", DbType.Int32, technicalTask.TechnicalTaskId);            
            _manager.AddInParameter("@Status", DbType.String, technicalTask.Status);
            _manager.AddInParameter("@TaskCompletedBy", DbType.String, technicalTask.TaskCompletedBy);
            
            return _manager.ExecuteNonQuery();
        }

        public int UpdateIncompleteTechTasks(ITechnicalTask inCompleteTechnicalTask)
        {
            _manager.StoredProcedureName = SP_UPDATE_INCOMPLETE_TECHNICALTASK;
            _manager.AddInParameter("@TechnicalTaskId", DbType.Int32, inCompleteTechnicalTask.TechnicalTaskId);
            _manager.AddInParameter("@SprintId", DbType.Int32, inCompleteTechnicalTask.SprintId);
            _manager.AddInParameter("@UserStoryId", DbType.Int16, inCompleteTechnicalTask.UserStoryId);
           
            return _manager.ExecuteNonQuery();
        }

        public ITechnicalTask GetTechnicalTask(int technicalTaskId)
        {
            _manager.StoredProcedureName = SP_GET_TECHNICAL_TASK;
            _manager.AddInParameter("@TechnicalTaskId", DbType.Int32, technicalTaskId);

            ITechnicalTask technicalTask = new TechnicalTask();
            Convert(technicalTask, _manager.ExecuteDataSet());
            return technicalTask;


        }
        public ITechnicalTask GetTechTaskWithUserStoryId(int userStoryId)
        {
            _manager.StoredProcedureName = SP_GET_TECHTASK_WITH_USERSTORYID;
            _manager.AddInParameter("@UserStoryId", DbType.Int32, userStoryId);
            ITechnicalTask technicalTask = new TechnicalTask();
            Convert(technicalTask, _manager.ExecuteDataSet());
            return technicalTask;
        }
        public IList<ITechnicalTask> GetTechnicalTasks(int userStoryId)
        {
            _manager.StoredProcedureName = SP_GET_TECHNICAL_TASKS;

            _manager.AddInParameter("@UserStoryId", DbType.Int32, userStoryId);

            IList<ITechnicalTask> technicalTasks = new List<ITechnicalTask>();
            foreach (ITechnicalTask technicalTask in ConvertAll(_manager.ExecuteDataSet()))
                technicalTasks.Add(technicalTask);
            return technicalTasks;


        }
        // public bool DeleteTechnicalTask(ITechnicalTask technicalTask)
        public int DeleteTechnicalTask(int technicalTaskId)
        {
            _manager.StoredProcedureName = SP_DELETE_TECHNICAL_TASK;
            _manager.AddInParameter("@TechnicalTaskId", DbType.Int32, technicalTaskId);
            return _manager.ExecuteNonQuery();
            // return true;

        }
        public IList<ITechnicalTask> getTechTaskWithSprintId(int sprintId)
        {
            _manager.StoredProcedureName = SP_GETTECHTASK_WITH_SPRINTID;
            _manager.AddInParameter("@SprintId", DbType.Int32, sprintId);
            IList<ITechnicalTask> technicalTasks = new List<ITechnicalTask>();
            foreach (ITechnicalTask technicalTask in ConvertAll(_manager.ExecuteDataSet()))
                technicalTasks.Add(technicalTask);
            return technicalTasks;


        }












    }
}

